import { defineStore } from "pinia"
import {ref,computed } from "vue"
import {useUserStore} from '@/stores/userStore'
import { findNewCartListAPI,insertCartAPI,delCartAPI} from "@/apis/cart"
import {updateCartItem,updateAllCheck} from '@/apis/cart'
export const useCartStore=defineStore('cart',()=>{
    // cartList,addCart,onMounted
    const cartList=ref([])
    // const addCart=(goods)=>{
    //     //判断商品是否在购物车
    //     const findItem=cartList.value.find((item)=> goods.skuId===item.skuId)
    //     if(findItem){
    //         findItem.count+=goods.count
    //     }else{
    //         cartList.value.push(goods)
    //     }

    // }
    // const delCart=(skuId)=>{
    //     const idx =cartList.value.findIndex((item)=>skuId===item.skuId)
    //     cartList.value.splice(idx,1)
    // }
      const delCart=async(skuId)=>{
        if(isLogin.value){ 
           await delCartAPI([skuId])  
           updateLoginCartList()
        }else{
            const idx =cartList.value.findIndex((item)=>skuId===item.skuId)
            cartList.value.splice(idx,1)
        }
    }

    // 计算属性
    // 1.总的数量  count之和
    const allCount=computed(()=> cartList.value.reduce((a,b)=>a+b.count,0))
    // 2.总价 count*price之和
    const allPrice=computed(()=> cartList.value.reduce((a,c)=>a+c.price*c.count,0))

    //单选功能
    const singleCheck=(skuId,selected)=>{
        const item =cartList.value.find(item=>item.skuId===skuId)
        item.selected=selected
    }
    //全选功能
    const checkAll=(selected)=>{
        cartList.value.forEach(item => item.selected = selected)
        
        // console.log(selected);
        
    }
    //是否权限计算器属性
    const isAll=computed(()=>cartList.value.every(item=>item.selected))
   // 4. 已选择数量
    const selectedCount = computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count, 0))
    // 5. 已选择商品价钱合计
    const selectedPrice = computed(() => cartList.value.filter(item => item.selected).reduce((a, c) => a + c.count * c.price, 0))
    
    const userStore=useUserStore()
    const isLogin=computed(()=>userStore.userInfo.token)

    const updateLoginCartList=async()=>{
        const res=await findNewCartListAPI()
        cartList.value=res.result
    }
    const addCart=async (goods)=>{
        if(isLogin.value){
            await insertCartAPI(goods)
            updateLoginCartList()
        }else{
            const item=cartList.value.find((item)=> goods.skuId===item.skuId)
                if(item){
                         item.count+=goods.count
                }else{
                        cartList.value.push(goods)
        }
        }
    }
    const clearCart=()=>{
        cartList.value=[]
    }
    //修改购物项
   const updateCart = async (goods) => {
    const {skuId,count,selected} = goods
    if (isLogin.value) {
        await updateCartItem(skuId,{count,selected})
    }
        }
    //全选框缓存
    const updateAllChecked=async(ids,selected_fan)=>{
        const selected=selected_fan?false:true

         if(isLogin.value){     
            await updateAllCheck({
                ids,
                selected
            })

         }
         checkAll(selected)
    }

    return{
        cartList,
        addCart,
        delCart,
        allCount,
        allPrice,
        singleCheck,
        isAll,
        checkAll,
        selectedCount,
        selectedPrice,
        updateLoginCartList,
        clearCart,
        updateCart,
        updateAllChecked
    }
},{
    persist:true
})